const config = require('../../utils/config.js');
import grace from "../../utils/grace.js"
grace.page({
      data: {
            order_id: 0,
            comment_rank: 5,
            content: '',
            empty_start: '../../image/start-empty.png',
            full_start: '../../image/start-full.png',
            img_url: [],
            img_origin_url: []
      },
      onLoad: function(options) {
            this.$data.order_id = options[0][0]['order_id'];
            wx.hideNavigationBarLoading();
      },
      bindSubmit: function() {
            let img_url_json = JSON.stringify(this.$data.img_url);
            let param = {
                  order_id: this.$data.order_id,
                  content: this.$data.content,
                  comment_rank: this.$data.comment_rank,
                  img_url_json: img_url_json
            }
            this.$http.post('api/comment/submit', param)
                  .then((res) => {
                        this.toast('操作成功！');
                        this.$goBack({
                              isUpdate: 1
                        });
                  });
      },
      chooseImage: function() {
            let api_token = wx.getStorageSync('api_token');
            var that = this;
            wx.chooseImage({
                  count: 1,
                  sizeType: ['compressed'],
                  sourceType: ['album', 'camera'],
                  success: function(res) {
                        wx.showToast({
                              title: '请等待..',
                              icon: 'loading',
                              duration: 2000
                        })
                        var tempFilePaths = res.tempFilePaths;
                        wx.uploadFile({
                              url: config.serviceUrl + 'api/home/upload_img', //仅为示例，非真实的接口地址
                              filePath: tempFilePaths[0],
                              name: 'img_data',
                              formData: {
                                    api_token: api_token
                              },
                              success: function(res) {
                                    wx.hideToast();
                                    if (res.statusCode == 200) {
                                          let data = JSON.parse(res.data);
                                          that.$data.img_url.push(data.img_url);
                                          that.$data.img_origin_url.push(data.img_origin_url);
                                    }
                                    if (res.statusCode == 400) {
                                          let data = JSON.parse(res.data);
                                          if (data) {
                                                wx.showToast({
                                                      title: data.message,
                                                      icon: 'none'
                                                })
                                          }
                                    }
                              },
                              fail: function(err) {
                                    that.toast('上传失败');
                              }
                        })
                  },
                  fail: function(err) {
                        console.error(err);
                  }
            });
      },
      changeColor: function(e) {
            this.$data.comment_rank = e.currentTarget.dataset.flag;
      },
      inputHandle: function(e) {
            this.$data.content = e.detail.value;
      },
})